package cn.com.callback

import groovy.json.*;
import groovy.swing.impl.DefaultAction
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import cn.com.callback.CallBackInvoke
import cn.com.callback.ServiceBackInvoke
import cn.com.elite.EliteBean;
import cn.com.elite.Util;
import java.security.MessageDigest;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.kh.getProperty;
import cn.com.kh.KHUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.json.JSONException;

/**
 *
 * @Title: apiTelHistory
 * @Description:深圳水务-live800查询客户联系历史接口--外部接口
 * @author: chieh
 * @date 2020-11-29 21:28:00
 *
 */
public class apiTelHistory implements ServiceBackInvoke {
	private static final Log log = LogFactory.getLog(apiTelHistory.class);

	public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {
		
		log.debug("-------------------------start apiTelHistory 开始-----------------");
		
		long startTime = System.currentTimeMillis();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=UTF-8");
		
		request.setCharacterEncoding("UTF-8");
		
		String reJson = null;
		long endTime = 0;
		
		def mobile = request.getParameter("mobile");//用户编号
		def timestamp = request.getParameter("timestamp");//用户编号
		def sign = request.getParameter("sign");//用户编号
		
		log.debug("解析信息-mobile：" + mobile+"	timestamp: " + timestamp+"	sign: " + sign);
		
		
		String apiid = getProperty.get("LIVE.apiid");
		String apikey = getProperty.get("LIVE.apikey");

		//String apiid = "CClive800";
		//String apikey = "4eb0187aafa24aae953df02c8eb1d1d9";
		String signfront = "";
		String signafter = ""
		
		signfront="apiid"+"="+apiid+"&"+"apikey"+"="+apikey+"&"+"timestamp"+"="+timestamp;
		log.debug("sign加密MD5前："+signfront);
		signafter=Util.getMD5Str(signfront);
		log.debug("sign加密MD5后："+signafter);
		if(!sign.equals(signafter)) {
			endTime = System.currentTimeMillis();
			reJson = "{\"resultcode\":\"0\",\"resultmessage\":\"处理失败(sign签名错误),调用问题处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":[]}";
			return reJson;
		}
		
		String ani = "";//来电号码
		String callstarttime = "";//开始时间
		String callendtime = "";//结束时间
		String calltypedescription = "";//来电类型
		String staffname = "";//话务员
		String rolegroupdescription = "";//话务组
		String caltype = "";//小结内容
		String comments = "";//小结备注
		
		try {
			def customerresult = eliteBean.chaxun_sql("select top 1 CUSTOMER_GUID from CUSTOMER  where mobile='"+mobile+"' ORDER BY CREATEDDATE asc", "dataSource");
			log.debug("查看客户是否存在sql结果:" + customerresult);
			String customer_guid = null;
			
			JSONArray resArray = new JSONArray();
			JSONObject resObject = new JSONObject();
			if(customerresult.size() > 0) {
				customer_guid = customerresult.get(0).get("CUSTOMER_GUID");
				log.debug("old_customer_guid" + customer_guid);
				
				List listcountresult =new ArrayList();
				listcountresult =eliteBean.chaxun_sql("select isnull(e.ani,'') as ani,isnull(e.callstarttime,'') as callstarttime,isnull(e.callendtime,'') as callendtime,isnull(cc.calltypedescription,'') as calltypedescription,isnull(s.staffname,'') as staffname,isnull(r.rolegroupdescription,'') as rolegroupdescription,isnull(case when tc.caltype = 'A0B4C5' then '报修报漏' when tc.caltype = 'F04391' then '咨询' when tc.caltype = 'EB386A' then '投诉' when tc.caltype = '3D1925' then '其他' end,'') as caltype,isnull(tc.comments,'') as comments from  event  e  left join  TABCcalsum tc  on e.event_guid=tc.relation_id and e.CALLTYPE='1' left join c_calltype cc on e.calltype=cc.calltype left join staff s on s.staff_id=e.handleby_id left join rolegroup r on r.rolegroup_id=e.handlegroup_id where 1=1 and e.customer_guid='"+customer_guid+"' and e.record_guid !='' and e.CALLTYPE='1' union all select isnull(e.dnis,'') as ani,isnull(e.callstarttime,'') as callstarttime,isnull(e.callendtime,'') as callendtime,isnull(cc.calltypedescription,'') as calltypedescription,isnull(s.staffname,'') as staffname,isnull(r.rolegroupdescription,'') as rolegroupdescription,isnull(case when tc.caltype = 'A0B4C5' then '报修报漏' when tc.caltype = 'F04391' then '咨询' when tc.caltype = 'EB386A' then '投诉' when tc.caltype = '3D1925' then '其他' end,'') as caltype,isnull(tc.comments,'') as comments from  event  e  left join  TABCcalsum tc  on e.event_guid=tc.relation_id and e.CALLTYPE='1' left join c_calltype cc on e.calltype=cc.calltype left join staff s on s.staff_id=e.handleby_id left join rolegroup r on r.rolegroup_id=e.handlegroup_id where 1=1 and e.customer_guid='"+customer_guid+"' and e.record_guid !='' and e.CALLTYPE='2' order BY callstarttime desc","dataSource");
				//获取总行数 and e.CALLSTARTTIME>=convert(varchar(10),dateadd(month,-12,cast(GETDATE() as DATE)),120) 
				def count =listcountresult.size();
				if(count > 0) {
					log.debug("sql获取总条数:"+count);
					if(listcountresult.size()>0) {
							for (int i = 0; i < count; i++) {
							ani = listcountresult.get(i).get("ani");
							callstarttime = listcountresult.get(i).get("callstarttime");
							callendtime = listcountresult.get(i).get("callendtime");
							calltypedescription = listcountresult.get(i).get("calltypedescription");
							staffname = listcountresult.get(i).get("staffname");
							rolegroupdescription = listcountresult.get(i).get("rolegroupdescription");
							caltype = listcountresult.get(i).get("caltype");
							comments = listcountresult.get(i).get("comments");
					
							resObject.put("ani", ani);
							resObject.put("callstarttime", callstarttime);
							resObject.put("callendtime", callendtime);
							resObject.put("calltypedescription", calltypedescription);
							resObject.put("staffname", staffname);
							resObject.put("rolegroupdescription", rolegroupdescription);
							resObject.put("caltype", caltype);
							resObject.put("comments", comments);
							log.debug("resObject："+resObject);
							
							resArray.add(resObject);
							}
						}

					log.debug("resObject"+resObject);					
					endTime = System.currentTimeMillis();
					reJson = "{\"resultcode\":\"1\",\"resultmessage\":\"查询接口调用成功,调用问题处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":["+resArray.toString()+"]}";
				}else{
					endTime = System.currentTimeMillis();
					reJson = "{\"resultcode\":\"1\",\"resultmessage\":\"查询接口调用成功(无相关记录),调用问题处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":[]}";
					return reJson;
				}
			}else{
				endTime = System.currentTimeMillis();
				reJson = "{\"resultcode\":\"1\",\"resultmessage\":\"查询接口调用成功(无当前客户),调用问题处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":[]}";
				return reJson;
			}
		} catch (Exception e) {
			log.error("接口处理异常" + e.getMessage());
			// 失败返回
			reJson = "{\"resultcode\":\"0\",\"resultmessage\":\"查询接口调用处理失败(" + e.getMessage() + ")\",\"data\":[]}";

		}
		return reJson;

	}
	
}